package com.wocao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.ReqBody;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.SysRole;
import com.wocao.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author Cwj
 * @since 2023-11-21
 */
@CrossOrigin
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    //查询+搜索
    @PostMapping("/selectAllPage/{page}")
    public ResponseResult<SysRole> selectAllPage(@PathVariable int page, @RequestBody ReqBody reqBody) {
        Page<SysRole> allPage = sysRoleService.selectAllPage(new Page(page, reqBody.getSize()), reqBody);
        return ResponseResult.ok(allPage);
    }

    //打开状态
    @PostMapping("/openStatus/{roleId}")
    public ResponseResult<SysRole> openStatus(@PathVariable long roleId) {
        sysRoleService.openStatus(roleId);
        return ResponseResult.ok("角色状态已打开");
    }

    //关闭状态
    @PostMapping("/closeStatus/{roleId}")
    public ResponseResult<SysRole> closeStatus(@PathVariable long roleId) {
        sysRoleService.closeStatus(roleId);
//        sysRoleService.removeByIds()
        return ResponseResult.ok("角色已停用");
    }

    //添加
    @PostMapping("/roleAdd")
    public ResponseResult roleAdd(@RequestBody ReqBody reqBody) {
        LocalDateTime now = LocalDateTime.now();
        reqBody.getRole().setCreateTime(now);
        sysRoleService.addRole(reqBody.getRole(), reqBody.getMenuIds());
        return ResponseResult.ok("添加成功");
    }

    //角色id查询菜单
    @GetMapping("/selectMenuIdsByRoleId/{roleId}")
    public ResponseResult selectMenuIdsByRoleId(@PathVariable long roleId) {
        List<Long> menuIdList = sysRoleService.selectMenuIdsByRoleId(roleId);
        return ResponseResult.ok(menuIdList);
    }

    //角色修改
    @PutMapping("/roleEdit")
    public ResponseResult roleEdit(@RequestBody ReqBody reqBody) {
        LocalDateTime now = LocalDateTime.now();
        reqBody.getRole().setUpdateTime(now);
        sysRoleService.roleEdit(reqBody.getRole(), reqBody.getMenuIds());
        return ResponseResult.ok("修改成功");
    }

    //角色删除
    @DeleteMapping("/deleteRole/{roleId}")
    public ResponseResult deleteRole(@PathVariable long roleId) {
        sysRoleService.deleteRole(roleId);
        return ResponseResult.ok("删除成功！！！");
    }

    //角色批量删除
    @DeleteMapping("/deleteRoles/{roleIds}")
    public ResponseResult deleteRoles(@PathVariable Object[] roleIds) {
        sysRoleService.deleteRoles(roleIds);
        return ResponseResult.ok("批量删除成功！！！");
    }

    //修改数据权限
    @PutMapping("/editRoleDept")
    public ResponseResult editRoleDept(@RequestBody ReqBody reqBody) {
        sysRoleService.editRoleDept(reqBody.getRole(), reqBody.getDeptIds());
        return ResponseResult.ok("数据权限修改成功");
    }

    //导出
    @GetMapping("/derive")
    public void derive(HttpServletResponse response, @RequestParam("roleIds") String[] roleIds) throws IOException {
        sysRoleService.derive(response, roleIds);
    }

    //查询所有可用角色
    @GetMapping("roles")
    public ResponseResult allRole() {
        List<SysRole> roleList = sysRoleService.list(new QueryWrapper<SysRole>().eq("del_flag", "0").eq("status", "0"));
        return ResponseResult.ok(roleList);
    }
}
